<template>
	<view class="path-container" style="background-color: #fff;height: 100%;">
		<view class="u-p-l-35 u-p-r-35">
			<Identify-Auth :registerType="0" ref="authUserName"></Identify-Auth>
		</view>
		<view class="position-bot">
			<set-submit-bt text="提交实名审核"  @click="lookCommitFn"/>
		</view>
		<akLoading isFullScreen :active="loading"></akLoading>
		<u-popup v-model="commitShow" mode="center"  class="commit-show" length="80%">
			<view class="commit-container u-p-30" >
				<view class="u-text-center u-font-36 u-m-b-30" style="font-weight: 500;">个人信息承诺书</view>
				<scroll-view scroll-y="true" style="height: 300rpx;">
					<view class="commit-content" >
						为响应国家号召，贯彻落实实名制，在此承诺遵守《关于加强网络信息保护的决定》、《电信和互联网用户个人信息保护规定》、《电话用户真实身份信息登记规定》等国家相关法律规定
						我承诺做到但不限于以下措施，以保障能够用完成在我方责任范围内的实名制责任与以上承诺。
						<br />
						1、用户应自行诚信向本平台提供注册资料，用户保证其提供的注册资料真实、准确、完整、合法有效，用户注册资料如有变动的，应及时更新其注册资料。如果用户提供的注册资料不合法、不真实、不准确、不详尽的，用户需承担因此引起的相应责任及后果。
						<br />2、用户在本平台进行浏览、下单购物等活动时，涉及用户真实姓名/名称、通信地址、联系电话、电子邮箱等隐私信息的，本平台将予以严格保密，除非得到用户的授权或法律另有规定，本平台不会向外界披露用户隐私信息。
						<br />3、用户注册成功后，可以根据本平台规定改变密码。用户应谨慎合理的保存、使用其用户名和密码，不得提供给其他任何人。用户若发现任何非法使用用户账号或存在安全漏洞的情况，请立即通知本平台并向公安机关报案。
						<br />4、用户不得将在本平台注册获得的账户借给他人使用，否则用户应承担由此产生的全部责任，并与实际使用人承担连带责任。
					</view>
				</scroll-view>
				<view class="commit-footer">
					<u-checkbox v-model="readSure" shape="circle" active-color="#ffbb1a">
						<text class="u-font-26" style="color: #999;">我已阅读并同意【个人信息承诺书】内容</text>
					</u-checkbox>
					<view class="u-flex u-row-between u-m-t-30">
						
						<view class="commit-btn" @click="closeCommit()">取消</view>
						<view :class="['commit-btn',{'timer-out':timer}]" @click="handleSubmit()">确认{{readTime}}</view>
					</view>
				</view>
			</view>
		</u-popup>
		<u-modal v-model="backHomeShow" :show-cancel-button="false"	@confirm="relaunchPage" :content="'已提交人工审核，请耐心等待~'" :mask-close-able="false" confirm-text="返回"></u-modal>
	</view>

</template>

<script>
	import IdentifyAuth from '@/components/Identify-Auth/index.vue';
	import SetSubmitBt from '@/components/set-submit-bt/index.vue'
	import { mapGetters } from "vuex";
	import { useUserStore } from '@/store/user.js';
	import { mapState } from 'pinia';
	export default{
		components:{IdentifyAuth,SetSubmitBt},
		data(){
			return {
				backHomeShow:false,
				loading:false,
				readSure:false,
				commitShow:false,
				readTime:'',
				timer:null,
			}
		},
		computed:{
			...mapState(useUserStore, {
				updateStatus: 'updateStatus',
				updateInfo: 'updateInfo',
				logout:'logout',
				login:'login',
				SET_USER_REALNAME:'SET_USER_REALNAME'
				})
		},
		created() {
			
		},
		methods:{
			closeCommit(){
				clearInterval(this.timer);
				this.timer = null;
				this.commitShow = false;
			},
			async lookCommitFn(){
				this.loading = true;
				let childData = this.$refs.authUserName;
				if(childData.userFrom.IdType === 1){
					if(this.$refs.authUserName.tabbarIndex === 0&&this.$refs.authUserName.idCardImageId== ''){
						this.loading = false;
						this.$u.toast('请先上传身份证信息');
						return false;;
					};
					if(this.$refs.authUserName.tabbarIndex === 0&&!this.$refs.authUserName.idCardIdentify){
						this.loading = false;
						this.$u.toast('未能识别出身份证信息，请重新上传识别');
						return false;;
					};
					const result = await this.$refs.authUserName.validateUser();
					this.loading = false;
					if(result){
						this.commitModalShow();
					}
				}else{
					if(!childData.userFrom.faceResourceId){
						this.loading = false;
						this.$u.toast('请先上传证件照正面照');
						return false;;
					};
					if(!childData.userFrom.backResourceId){
						this.loading = false;
						this.$u.toast('请先上传证件照反面照');
						return false;;
					};
					if(!childData.userFrom.realname){
						this.loading = false;
						this.$u.toast('姓名不得为空');
						return false;;
					};
					if(!childData.userFrom.idCardNo){
						this.loading = false;
						this.$u.toast('证件号码不得为空');
						return false;;
					};
					this.commitModalShow();
				}
			},
			// 打开用户协议弹窗
			commitModalShow(){
				let count = 5;
				let that = this;
				if(this.timer != null){
					clearInterval(this.timer);
					this.timer = null;
				}
				this.timer = setInterval(function(){
					if(count<=1){
						clearInterval(that.timer);
						that.timer = null;
						that.readTime = '';
						return false;
					}
					count--;
					that.readTime = '('+count+'s)';
				},1000);
				that.readTime = '('+count+'s)';
				this.commitShow = true;
			},
			relaunchPage(){
				this.updateStatus(0)
				this.$http.get('api/User/GetUserClaim').then(info=>{
					this.updateInfo(info)
					this.$mHelper.delayedJump('/pages/mine/index', 'reLaunch');
				}).catch(err=>{
					this.logout()
					uni.redirectTo({
						url: '/'
					});
				});
			},
			async handleSubmit() {
				if(!this.readSure){
					return this.$u.toast('请点击同意个人承诺书')
				}
				if(this.timer!=null){
					return false;
				}
				this.loading = true;
				this.commitShow = false;
				const res = await this.$newHttp.post('api/User/RealNameAuthentication', this.$refs.authUserName.userFrom);
				this.loading =false;
				if (res.code == 200){
					if(this.$refs.authUserName.userFrom.IdType === 1){
						this.SET_USER_REALNAME(true);
						this.updateStatus(0)
						// 获取该用户得店铺
						const result = await this.$newHttp.get('api/User/GetShopsByUser' );
						if(result.code === 200){
							if (result.data.length!==0) {
								await this.login(result.data)
							}
						}
						uni.showToast({
							title:'实名认证成功'
						})
						this.$mHelper.delayedJump('/pages/mine/index', 'reLaunch');
					}else{
						this.backHomeShow = true;
					}
				}else if(res.code === 30904){
					this.$u.toast('自动认证失败，已转为人工审核，请耐心等候~');
					this.$mHelper.delayedJump('/pages/mine/index', 'reLaunch');
					this.updateStatus(0)
				}else{
					this.$u.toast('实名认证失败，错误信息：'+res.erroMessage);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #fff !important;
	}
	.path-container{
		.register-process{
			background: #F6F6F6;
			border-radius: 10rpx;
			.process-container{
				.process-mark{
					width: 36rpx;
					height: 36rpx;
					background: #FFBB1A;
					border-radius: 50%;
				}
				.process-line{
					width: 126rpx;
					height: 4rpx;
					border-radius: 2rpx;
					background-color: #FFBB1A;
				}
				.mark-grey{
					background-color: #D5D5D5 !important;
				}
			}
		}
		.commit-show{
			::v-deep .u-mode-center-box{
				border-radius: 14rpx;
			}
			.commit-btn{
				width: 46%;
				box-sizing: border-box;
				padding: 20rpx 0;
				border-radius:70rpx;
				text-align: center;
			}
			.commit-btn:nth-of-type(1){
				border: 1px solid #333;
			}
			.commit-btn:nth-of-type(2){
				background: #FFBB1A;
			}
			.timer-out{
				background:#ccc !important;
			}
		}
	}
</style>